package edu.cmu.cs.webapp.ioms.ui.wizards.order;

import java.sql.Date;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.cmu.cs.webapp.ioms.models.OrderStatus;
import edu.cmu.cs.webapp.ioms.models.PurchaseOrder;
import edu.cmu.cs.webapp.ioms.utils.IOMSConstants;

public class OrderMainWizardPage extends WizardPage {

	// private Text _orderIDTxt;
	private DateTime _orderDateTxt;
	private Combo _statusTxt;
	private DateTime _shippingDate;
	private Text _shippingAddr;
	private Text _shippingCity;
	private Combo _shippingState;
	private Text _shippingCountry;

	public OrderMainWizardPage(String pageName) {
		super(pageName);
		setTitle("Order Main");
		setDescription("Input the general infomation of the new order. \nNotice: All fields with asterisk(*) are required");

	}

	public OrderMainWizardPage(String pageName, String title,
			ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);

		layout.numColumns = 2;
		// layout.verticalSpacing = 9;
		/* id */
		//		label.setText("Order ID*:"); //$NON-NLS-1$
		//
		// _orderIDTxt = new Text(container, SWT.BORDER | SWT.SINGLE);
		//
		// GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		// _orderIDTxt.setLayoutData(gd);
		// _orderIDTxt.addModifyListener(new ModifyListener() {
		// public void modifyText(ModifyEvent e) {
		// validate();
		// }
		// });
		// this._orderIDTxt.setEnabled(false);
		// this._orderIDTxt.setEditable(false);
		// this._orderIDTxt.setEditable(false);

		Label label = new Label(container, SWT.NULL);
		label.setText("Order Status:"); //$NON-NLS-1$
		_statusTxt = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		_statusTxt.setLayoutData(gd);

		label = new Label(container, SWT.NULL);
		label.setText("Order Date*:"); //$NON-NLS-1$
		_orderDateTxt = new DateTime(container, SWT.BORDER | SWT.DROP_DOWN);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		_orderDateTxt.setLayoutData(gd);

		createLine(container, layout.numColumns);

		label = new Label(container, SWT.NULL);
		label.setText("Expected Shipping Date:"); //$NON-NLS-1$

		_shippingDate = new DateTime(container, SWT.BORDER | SWT.DROP_DOWN);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		_shippingDate.setLayoutData(gd);

		label = new Label(container, SWT.NULL);
		label.setText("Shipping Address:"); //$NON-NLS-1$

		_shippingAddr = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		_shippingAddr.setLayoutData(gd);
		_shippingAddr.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("Shipping City:"); //$NON-NLS-1$

		_shippingCity = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		_shippingCity.setLayoutData(gd);
		_shippingCity.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("Shipping State:"); //$NON-NLS-1$

		_shippingState = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		_shippingState.setLayoutData(gd);
		_shippingState.setItems(IOMSConstants.US_STATES);
		_shippingState.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("Shipping Country:"); //$NON-NLS-1$

		_shippingCountry = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		_shippingCountry.setLayoutData(gd);
		_shippingCountry.setText("US");
		_shippingCountry.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		if (isUpdate()) {
			loadFromExistingModel();
		} else {
			_statusTxt
					.setItems(new String[] { OrderStatus.PENDING.getLiteral() });
			_statusTxt.select(0);
			_statusTxt.setEnabled(false);
		}

		setControl(container);
		validate();
		container.pack(true);

	}

	private void loadFromExistingModel() {
		PurchaseOrder model = getModel();
		if (model != null) {

			_orderDateTxt.setDate(model.getOrder_date().getYear() + 1900, model
					.getOrder_date().getMonth(), model.getOrder_date()
					.getDate());

			if (model.getStatus().equals(OrderStatus.PENDING)) {
				_statusTxt.setItems(new String[] { OrderStatus.PENDING
						.getLiteral() });
				_statusTxt.setEnabled(false);
			} else {
				_statusTxt.setItems(new String[] {
						OrderStatus.ACCEPTED.getLiteral(),
						OrderStatus.INPROCESS.getLiteral(),
						OrderStatus.DELIVERED.getLiteral() });
			}
			_statusTxt.setText(model.getStatus().getLiteral());

			_shippingAddr.setText(model.getShipping_address() == null ? ""
					: model.getShipping_address());
			_shippingDate.setDate(
					model.getExpected_shipping_date().getYear() + 1900, model
							.getExpected_shipping_date().getMonth(), model
							.getExpected_shipping_date().getDate());
			_shippingCity.setText(model.getShipping_city() == null ? "" : model
					.getShipping_city());
			_shippingCountry.setText(model.getShipping_country() == null ? ""
					: model.getShipping_country());
		}
	}

	private void createLine(Composite parent, int ncol) {
		Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.BOLD);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = ncol;
		line.setLayoutData(gridData);
	}

	private boolean validate() {
		boolean valid = true;
		String errorMessage = null;

		if (valid && (getName() == null || getName().length() == 0)) {
			valid = false;
			errorMessage = "Name field could not be empty";
		}

		if (valid) {
			setErrorMessage(null);
			setPageComplete(true);
		} else {
			setErrorMessage(errorMessage);
			setPageComplete(false);
		}

		return valid;

	}

	public String getShippingCountry() {
		return this._shippingCountry.getText().trim();
	}

	public String getStatus() {
		return this._statusTxt.getText().trim();
	}

	public Date getOrderDate() {
		return new Date(this._orderDateTxt.getYear() - 1900, this._orderDateTxt
				.getMonth(), this._orderDateTxt.getDay());
	}

	public Date getShippingDate() {
		return new Date(this._shippingDate.getYear() - 1900, this._shippingDate
				.getMonth(), this._shippingDate.getDay());
	}

	public String getShippingAddress() {
		return this._shippingAddr.getText().trim();

	}

	public String getShippingCity() {
		return this._shippingCity.getText().trim();

	}

	public String getShippingState() {
		return this._shippingState.getText().trim();
	}

	private boolean isUpdate() {
		return ((OrderWizard) getWizard()).isUpdate();
	}

	private PurchaseOrder getModel() {
		return ((OrderWizard) getWizard()).getModel();

	}

}
